package com.fortune.rainshine.controller;

import com.fortune.rainshine.feign.service.OrderFeign;
import com.fortune.rainshine.feign.service.ShopFeign;
import com.fortune.rainshine.feign.system.UserFeign;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.ShopModel;
import com.fortune.rainshinecommon.model.service.OrderModel;
import com.fortune.rainshinecommon.model.system.User;
import com.fortune.rainshinecommon.query.OrderQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: rainShine
 * @description: 订单接口
 * @author: tangyn
 * @create: 2019-05-29
 **/
@RestController
@RequestMapping("OrderController")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private ShopFeign shopFeign;

    @Autowired
    private UserFeign userFeign;

    /**
     * 卖方确认出货
     * @param query
     * @return
     */
    @RequestMapping("/confirmingOutput")
    @ResponseBody
    public MsgModel confirmingOutput(@RequestBody OrderQuery query){
        ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        String userId = request.getHeader("userId");
        try{
            OrderModel orderModel = orderFeign.getByOrderCode(query.getOrderCode());
            if(orderModel == null){
                return new MsgModel("订单不存在！", ResultCode.FAILURE);
            }
            if(orderModel.getOrderStatus().intValue() != 1){
                return new MsgModel("订单状态异常，请刷新页面", ResultCode.FAILURE);
            }
//            if(query.getUserId() == null){
//                return new MsgModel("用户登录状态异常，请重新登录", ResultCode.FAILURE);
//            }
            ShopModel shopModel = shopFeign.getShopByShopId(orderModel.getShopIdFrom());
            if(Integer.valueOf(userId) != shopModel.getManagerId()){
                return new MsgModel("无此操作权限！", ResultCode.FAILURE);
            }
            User user = userFeign.getUserById(Integer.valueOf(userId));
            orderModel.setUser(user);
            return orderFeign.confirmingAnOrder(orderModel);
        }catch (Exception e){
            logger.error("confirmingOutput ====> {}", e);
            return new MsgModel("确认出货异常", ResultCode.FAILURE);
        }
    }

    /**
     * 买方确认收货
     * @param query
     * @return
     */
    @RequestMapping("/confirmingInput")
    @ResponseBody
    public MsgModel confirmingInput(@RequestBody OrderQuery query){
        ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        String userId = request.getHeader("userId");
        try{
            OrderModel orderModel = orderFeign.getByOrderCode(query.getOrderCode());
            if(orderModel == null){
                return new MsgModel("订单不存在！", ResultCode.FAILURE);
            }
            if(orderModel.getOrderStatus().intValue() != 2){
                return new MsgModel("订单状态异常，请刷新页面", ResultCode.FAILURE);
            }
            if(query.getUserId() == null){
                return new MsgModel("用户登录状态异常，请重新登录", ResultCode.FAILURE);
            }
            ShopModel shopModel = shopFeign.getShopByShopId(orderModel.getShopId());
            if(Integer.valueOf(userId) != shopModel.getManagerId()){
                return new MsgModel("无此操作权限！", ResultCode.FAILURE);
            }
            User user = userFeign.getUserById(Integer.valueOf(userId));
            orderModel.setUser(user);
            return orderFeign.receivingAnOrder(orderModel);
        }catch (Exception e){
            logger.error("confirmingInput ====> {}", e);
            return new MsgModel("确认出货异常", ResultCode.FAILURE);
        }
    }

    /**
     * 买方取消订单
     * @param query
     * @return
     */
    @RequestMapping("/cancelOrder")
    @ResponseBody
    public MsgModel cancelOrder(@RequestBody OrderQuery query){
        try{
            OrderModel orderModel = orderFeign.getByOrderCode(query.getOrderCode());
            if(orderModel == null){
                return new MsgModel("订单不存在！", ResultCode.FAILURE);
            }
            if(orderModel.getOrderStatus().intValue() != 1){
                return new MsgModel("订单状态异常，请刷新页面", ResultCode.FAILURE);
            }
            if(query.getUserId() == null){
                return new MsgModel("用户登录状态异常，请重新登录", ResultCode.FAILURE);
            }
            ShopModel shopModel = shopFeign.getShopByShopId(orderModel.getShopId());
            if(query.getUserId() != shopModel.getManagerId()){
                return new MsgModel("无此操作权限！", ResultCode.FAILURE);
            }
            orderModel.setOrderStatus(Byte.valueOf("5")); //5 - 买方取消订单
            return orderFeign.cancelOrder(orderModel);
        }catch (Exception e){
            logger.error("cancelOrder ====> {}", e);
            return new MsgModel("取消订单异常", ResultCode.FAILURE);
        }
    }

    /**
     * 卖方拒绝订单
     * @param query
     * @return
     */
    @RequestMapping("/refuseOrder")
    @ResponseBody
    public MsgModel refuseOrder(@RequestBody OrderQuery query){
        try{
            OrderModel orderModel = orderFeign.getByOrderCode(query.getOrderCode());
            if(orderModel == null){
                return new MsgModel(ResultCode.FAILURE, "订单不存在！");
            }
            if(orderModel.getOrderStatus() != 1){
                return new MsgModel(ResultCode.FAILURE, "订单状态异常，请刷新页面");
            }
            if(query.getUserId() == null){
                return new MsgModel(ResultCode.FAILURE, "用户登录状态异常，请重新登录");
            }
            ShopModel shopModel = shopFeign.getShopByShopId(orderModel.getShopIdFrom());
            if(query.getUserId() != shopModel.getManagerId()){
                return new MsgModel(ResultCode.FAILURE, "无此操作权限！");
            }
            orderModel.setOrderStatus(Byte.valueOf("6")); //6 - 卖方拒绝订单
            return orderFeign.cancelOrder(orderModel);
        }catch (Exception e){
            logger.error("refuseOrder ====> {}", e);
            return new MsgModel(ResultCode.FAILURE, "拒绝订单异常");
        }
    }
}
